package leetcode.code0020;

import java.util.Stack;

/**
 * CharAt效果一样
 * @author CaoYu
 * @date 2021年9月26日
 * @description
 */
public class Solution1 extends Solution0020 {

	@Override
	public boolean isValid(String s) {
		if (s.length() < 2) {
			return false;
		}
		Stack<Character> stack = new Stack<>();
		for (int i = 0; i < s.length(); i++) {
			char ci = s.charAt(i);
			if (ci == '(' || ci == '[' || ci == '{') {
				stack.add(ci);
			} else {
				if (stack.isEmpty())
					return false;
				char c = stack.pop();
				if ((ci == ')' && c != '(') || (ci == ']' && c != '[') || (ci == '}' && c != '{'))
					return false;
			}
		}
		return stack.isEmpty();
	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
	}

}
